package com.darren.archive.service.impl;

import com.darren.archive.mapper.SysUserMapper;
import com.darren.archive.model.SysUser;
import com.darren.archive.service.ISysUserService;
import com.darren.archive.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)//默认该类的所有方法都开启事务，遇到Exception就回滚
public class SysUserServiceImpl implements ISysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 保存用户信息
     * @param record 用户信息
     * @return 用户信息
     */
    @Override
    public SysUser save(SysUser record) {
        if(record == null){
            throw new ArithmeticException("参数错误，没有要保存的数据。");
        }
        if(StringUtils.isEmpty(record.getUserAccount())){
            throw new ArithmeticException("用户帐号不能为空。");
        }
        if(record.getId() == null && sysUserMapper.existsUserAccount(record.getUserAccount())){
            throw new ArithmeticException(String.format("用户帐号【%s】已存在。",record.getUserAccount()));
        }
        if(StringUtils.isNotEmpty(record.getMobile()) && sysUserMapper.existsUserMobile(record.getId(),record.getMobile())){
            throw new ArithmeticException(String.format("该手机号【%s】号已被其它用户使用。",record.getMobile()));
        }
        if(StringUtils.isNotEmpty(record.getEmail()) && sysUserMapper.existsUserMobile(record.getId(),record.getEmail())){
            throw new ArithmeticException(String.format("该邮箱【%s】号已被其它用户使用。",record.getEmail()));
        }
        if(record.getId() == null){
            sysUserMapper.insert(record);
        }else{
            sysUserMapper.updateByPrimaryKey(record);
        }
        record = sysUserMapper.selectByPrimaryKey(record.getId());
        return record;
    }

    /**
     * 批量新增用户信息，常用于导入
     * @param records  用户信息
     * @return  用户信息
     */
    @Override
    public List<SysUser> batchAdd(List<SysUser> records) {
        if(records == null || records.size() == 0){
            throw new ArithmeticException("参数错误，没有要保存的数据。");
        }
        List<SysUser> resultRecords = new ArrayList<SysUser>();
        for(SysUser record:records){
            if(record != null){
                record = this.save(record);
                resultRecords.add(record);
            }
        }
        return resultRecords;
    }


    /**
     * 通过主键id删除数据
     *
     * @param id  主键id
     * @return    受影响的数据的行数
     */
    @Override
    public Integer deleteById(Long id) {
        return sysUserMapper.deleteByPrimaryKey(id,null);
    }

    /**
     * 通过用户账号删除数据
     *
     * @param userAccount 用户账号
     * @return 受影响的数据的行数
     */
    @Override
    public Integer deleteByUserAccount(String userAccount) {
        return sysUserMapper.deleteByUserAccount(userAccount,null);
    }


    /**
     * 根据主键id 获取用户信息
     * @param id 主键id
     * @return   用户信息
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)//不开启事务的支持
    public SysUser getByPrimaryKey(Long id) {
        return sysUserMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据用户账号，获取用户信息
     *
     * @param userAccount 用户账号
     * @return 用户信息
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)//不开启事务的支持
    public SysUser getByUserAccount(String userAccount) {
        return sysUserMapper.selectByUserAccount(userAccount);
    }

    /**
     * 使用账号、手机号、邮箱和密码登陆
     *
     * @param accountOrMobileOrEmail 账号、手机号、邮箱
     * @param password               用户密码
     * @return 用户信息
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)//不开启事务的支持
    public SysUser loginByAccountOrMobileOrEmailAndPassword(String accountOrMobileOrEmail, String password) {
        if(StringUtils.isEmpty(accountOrMobileOrEmail)){
            throw new ArithmeticException("用户帐号不能为空。");
        }
        if(StringUtils.isEmpty(password)){
            throw new ArithmeticException("用户密码不能为空。");
        }
        SysUser user = sysUserMapper.selectByUserAccountOrMobileOrEmail(accountOrMobileOrEmail,true);
        if(user == null){
            throw new ArithmeticException(String.format("用户【%s】不存在。",accountOrMobileOrEmail));
        }
        if(!password.equals(user.getPassword())){
            throw new ArithmeticException("用户密码错误。");
        }
        return user;
    }

    /**
     * 根据用户部门、用户帐号、用户姓名、手机号、邮箱查询用户
     *
     * @param departmentId     用户部门
     * @param userAccount      用户帐号
     * @param userName         用户姓名
     * @param mobile           手机号
     * @param email            邮箱
     * @return                 用户信息
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)//不开启事务的支持
    public List<SysUser> getByConditions(Long departmentId,String userAccount,String userName,String mobile,String email) {
        return sysUserMapper.selectByConditions(departmentId,userAccount,userName,mobile,email);
    }
}
